Ext.namespace('VAS');

VAS.DynamicCombo=Ext.extend(Ext.form.ComboBox,{
 initComponent : function() {
 	var _this=this;
 	
 	_this._editable=this._editable?true:false;
 	
 	_this._url=this.url;
 	
	_this.proxyDs= new Ext.data.Store({
						proxy: new Ext.data.HttpProxy({url:_this._url}),
						reader: new Ext.data.JsonReader({root:'data.combo'}, [
							 _this.valueField,
							 _this.displayField,
							 _this.iconClsField
						])
					});
	//如果为动态加载数据，则load一下。				
	if(!this.staticFlag){
		_this.proxyDs.load();
	}
					
	_this._plugins=this.iconCombo?new Ext.ux.plugins.IconCombo():[];
	
	_this.staticDs=new Ext.data.SimpleStore({
                    fields: [ _this.valueField,
							 _this.displayField,
							 _this.iconClsField],
                    data: _this.getData(_this.staticData)
            });
            
	_this.ds=this.staticFlag?_this.staticDs:_this.proxyDs;
	_this._mode=this.staticFlag?'local':'remote';
	if(_this.width !== '0'){		//如果为0,表示没有设置宽度，则取默认值
		_this.width =  parseInt(_this.width);  //把width由stirng 转为number
	}
 	var config={
 		store: _this.ds,
        editable: _this._editable,
//        layout:'form',
        forceSelection: true,
        defaults:{anchor:'100%'},
        bodyStyle:'padding:10px' ,
        triggerAction: 'all',
        emptyText:'请选择...',
        selectOnFocus:true,
        hiddenName: _this.name,
        name:_this.name,
        fieldLabel:_this.fieldLabel,
        valueField: _this.valueField,
		displayField:  _this.displayField,
		iconClsField:_this.iconClsField,
		width: _this.width,
        minChars :_this.minChars
        ,plugins:_this._plugins
        ,mode:_this._mode
        ,lazyInit:false
        
      };
 	
 Ext.apply(this, config);
 Ext.apply(this.initialConfig, config);
 VAS.DynamicCombo.superclass.initComponent.apply(this, arguments);
 	
 },

onRender:function(ct, position){
	var _this=this;
	VAS.DynamicCombo.superclass.onRender.call(this, ct, position);	
	_this.setValue(_this.value);	//设置默认值
	if(!this.staticFlag&&this.loadForce){
		this.store.load();
	}
},

getData:function(_staticData){
   var _arrayData=[];
	for(var i=0;i<_staticData.length;i++){
		var _objdata=[];
		var obj={};
		obj=_staticData[i];
		
		if(typeof(obj[this.iconClsField])!=='undefined'){
			_objdata=[obj[this.valueField],obj[this.displayField],obj[this.iconClsField]]
		
		}else{
			_objdata=[obj[this.valueField],obj[this.displayField]]
		}
		_arrayData.push(_objdata);
	}
	return _arrayData;
}

});

Ext.reg('dynamicCombo',VAS.DynamicCombo);